package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
//套餐管理
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/upload")
    public Result uploadImg(@RequestParam("imgFile") MultipartFile imgFile ){
//        System.out.println(imgFile);
//        得到文件的原始名字
//        System.out.println(imgFile.getOriginalFilename());
//        得到文件的后缀
        String ext =imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf("."));
        String imgName = UUID.randomUUID().toString()+ext;
        try {
            InputStream inputStream = imgFile.getInputStream();
            AliyunOSSUtil.uploadFileByStream( "imgs/"+imgName,inputStream);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,imgName);

    }


    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

            return setmealService.findPage(queryPageBean);

    }
    @RequestMapping("/findSetmealById")
    public Result findSetmealById(Integer id){
        try {
            Setmeal setmealDetail = setmealService.findSetmealById(id);
            return  new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmealDetail);
        } catch (Exception e) {

            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/findCheckgroupBySetmealId")
    public Result findCheckgroupBySetmealId(Integer id ){
        try {
            List<Integer> checkgroupsId=setmealService.findCheckgroupBySetmealId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupsId);
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);

        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[]  checkgroupIds ){
        try {
            setmealService.edit(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);

        }
    }
    @RequestMapping("/deleteImg")
    public void deleteImg(String imageNameOld){
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imageNameOld);
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,imageNameOld);

    }

    @RequestMapping("/deleteSetmealById")
    public Result deleteSetmealById(Integer id ){
        try {
            setmealService.deleteSetmealById(id);
//        找到了删除行对应的实体数据
            Result setmealById = this.findSetmealById(id);
            Setmeal setmeal = (Setmeal)setmealById.getData();
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal.getImg());
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
